// 冰冻效果Shader
Shader "Custom/IceEffect"
{
    Properties {
        _MainColor ("冰面主颜色", Color) = (0.7,0.9,1,0.5)
        _FrostColor ("霜冻颜色", Color) = (1,1,1,0.3)
        _NoiseTex ("噪声贴图", 2D) = "white" {}
        _IceScale ("冰面缩放", Range(0.1,10)) = 2.0
        _RefractStrength ("折射强度", Range(0,1)) = 0.5
        _Specular ("高光强度", Range(0,5)) = 1.5
        _Gloss ("光泽度", Range(0,1)) = 0.8
        _FrostAmount ("霜冻程度", Range(0,1)) = 0.3
    }

    SubShader {
        Tags { "Queue"="Transparent" "RenderType"="Transparent" }
        LOD 200

        GrabPass {}

        CGPROGRAM
        #pragma surface surf StandardSpecular alpha:fade
        #pragma target 3.0

        struct Input {
            float2 uv_MainTex;
            float2 uv_BumpMap;
            float3 viewDir;
            float4 screenPos;
        };

        // 在CGPROGRAM部分添加声明
        sampler2D _NoiseTex;
        half _IceScale;
        sampler2D _GrabTexture;
        fixed4 _MainColor;
        // Removed duplicate _IceScale declaration
        fixed4 _FrostColor;
        half _RefractStrength;
        half _Specular;
        half _Gloss;
        half _FrostAmount;

        void surf (Input IN, inout SurfaceOutputStandardSpecular o) {
            // 基础纹理采样
            // 程序化法线生成
            float2 noiseUV = IN.uv_MainTex * _IceScale;
            fixed4 noise = tex2D(_NoiseTex, noiseUV);
            
            // 基于噪声生成法线细节
            float3 normal = float3(noise.g - 0.5, noise.b - 0.5, 1);
            normal = normalize(normal);
            
            // 生成冰裂基础颜色
            float icePattern = saturate(noise.r * 2 - 0.5);

            // 折射效果计算
            float2 refractUV = (IN.screenPos.xy / IN.screenPos.w) + normal.xy * _RefractStrength;
            fixed3 refractColor = tex2D(_GrabTexture, refractUV);

            // 表面霜冻混合
            float rim = 1 - saturate(dot(normalize(IN.viewDir), normal));
            fixed3 frostEffect = _FrostColor.rgb * pow(rim, 2) * _FrostAmount;

            // 最终颜色合成
            o.Albedo = lerp(_MainColor.rgb * _MainColor.rgb, refractColor, 0.3);
            o.Normal = normal;
            o.Specular = _Specular;
            o.Smoothness = _Gloss;
            o.Alpha = _MainColor.a;
            o.Emission = frostEffect;
        }
        ENDCG
    }
    FallBack "Diffuse"
}